package ezy.widget.view;

import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import ezy.widget.R;


class LabelContext {
    private View mParent;

    private float mBaseline;
    private float mLabelX;
    private Paint mLabelPaint;
    private String mLabelText;
    private ColorStateList mLabelColor;

    private int mPaddingRight = 0;
    private float mSuffixWidth = 0;
    private Paint mSuffixPaint;
    private String mSuffixText;
    private ColorStateList mSuffixColor;

    LabelContext(View parent, AttributeSet attrs) {
        this.mParent = parent;
        TypedArray a = parent.getContext().obtainStyledAttributes(attrs, R.styleable.LabelEditText);
        float defaultTextSize = parent instanceof TextView
                ? ((TextView) parent).getTextSize()
                : TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, Resources.getSystem().getDisplayMetrics());
        float labelSize = defaultTextSize, suffixSize = defaultTextSize;
        float labelWidth = 0, suffixWidth = 0;
        int labelStyle = -1, suffixStyle = -1;
        int labelTypeface = -1, suffixTypeface = -1;
        String labelTypefacePath = null, suffixTypefacePath = null;
        try {
            mLabelText = a.getString(R.styleable.LabelEditText_labelText);
            mLabelColor = a.getColorStateList(R.styleable.LabelEditText_labelColor);
            labelSize = a.getDimension(R.styleable.LabelEditText_labelSize, labelSize);
            labelWidth = a.getDimension(R.styleable.LabelEditText_labelWidth, labelWidth);
            labelStyle = a.getInteger(R.styleable.LabelEditText_labelStyle, -1);
            labelTypefacePath = a.getString(R.styleable.LabelEditText_labelTypeface);
            try {
                labelTypeface = a.getInteger(R.styleable.LabelEditText_labelTypeface, -1);
            } catch (Exception e) {

            }
            mSuffixText = a.getString(R.styleable.LabelEditText_suffixText);
            mSuffixColor = a.getColorStateList(R.styleable.LabelEditText_suffixColor);
            suffixSize = a.getDimension(R.styleable.LabelEditText_suffixSize, suffixSize);
            mSuffixWidth = a.getDimension(R.styleable.LabelEditText_suffixWidth, suffixWidth);
            suffixStyle = a.getInteger(R.styleable.LabelEditText_suffixStyle, -1);
            suffixTypefacePath = a.getString(R.styleable.LabelEditText_suffixTypeface);
            try {
                suffixTypeface = a.getInteger(R.styleable.LabelEditText_suffixTypeface, -1);
            } catch (Exception e) {

            }
        } finally {
            a.recycle();
        }

        mLabelX = parent.getPaddingLeft();
        mLabelPaint = PaintUtil.createTextPaint(parent.getContext(), labelSize, labelStyle, labelTypeface, labelTypefacePath);
        setPaintColor(mLabelPaint, mLabelColor);
        mSuffixPaint = PaintUtil.createTextPaint(parent.getContext(), suffixSize, suffixStyle, suffixTypeface, suffixTypefacePath);
        setPaintColor(mSuffixPaint, mSuffixColor);
        mPaddingRight = parent.getPaddingRight();
        parent.setPadding(parent.getPaddingLeft() + (int) labelWidth, parent.getPaddingTop(), parent.getPaddingRight() + (int) mSuffixWidth, parent.getPaddingBottom());

    }

    void setLabelText(String value) {
        mLabelText = value;
        mParent.invalidate();
    }
    void setSuffixText(String value) {
        mSuffixText = value;
        mParent.invalidate();
    }

    private void setPaintColor(Paint paint, ColorStateList textColor) {
        if (paint == null) {
            return;
        }
        if (textColor != null) {
            paint.setColor(textColor.getColorForState(mParent.getDrawableState(), 0));
        } else if (mParent instanceof TextView) {
            paint.setColor(((TextView) mParent).getCurrentTextColor());
        }
    }
    void drawableStateChanged() {
        setPaintColor(mLabelPaint, mLabelColor);
        setPaintColor(mSuffixPaint, mSuffixColor);
    }

    void onDraw(Canvas canvas) {
        if (mBaseline == 0) {
            Paint.FontMetrics metrics = mLabelPaint.getFontMetrics();
            mBaseline = (mParent.getMeasuredHeight() - metrics.bottom - metrics.top) / 2;
        }
        if (mLabelText != null) {
            canvas.drawText(mLabelText, mLabelX + mParent.getScrollX(), mBaseline, mLabelPaint);
        }
        if (mSuffixText != null) {
            Rect rect = new Rect();
            mSuffixPaint.getTextBounds(mSuffixText, 0, mSuffixText.length(), rect);
            int suffixX = mParent.getMeasuredWidth() - mPaddingRight - rect.width();
            canvas.drawText(mSuffixText, suffixX, mBaseline, mSuffixPaint);
        }
    }
}